//
//  MyselfViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MyselfViewController.h"
#import "GoInViewController.h"
#import "MyActiviCollection.h"
#import "MyMovieCollectionViewController.h"
#import "LXC_DataBaseHandle.h"
#import "UserModel.h"

@interface MyselfViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain)  NSArray *arrTitle;

@property (nonatomic, assign) NSInteger goinState;

@end

@implementation MyselfViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.navigationItem.title = @"我的";
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
    imgview.image = [UIImage imageNamed:@"123.jpg"];
    _tableView.tableHeaderView = imgview;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    self.arrTitle = [NSArray arrayWithObjects:@"我的活动", @"我的电影", @"清除缓存", nil];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    NSString *rightButtonTitle = @"";
    
    self.goinState = [[LXC_DataBaseHandle shareDataBaseHandle] selectLoginStateFromUserTab:1];
    
    if (_goinState) {
        
        rightButtonTitle = @"注销";
        
    }else {
        rightButtonTitle = @"登录";
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:rightButtonTitle style:(UIBarButtonItemStylePlain) target:self action:@selector(goInAction:)];
    
}

- (void)goInAction:(UIBarButtonItem *)button {
    self.goinState = [[LXC_DataBaseHandle shareDataBaseHandle] selectLoginStateFromUserTab:1];
    
    if (!_goinState) {
        
        // 如果是未登录状态  推到登录页面
        
        GoInViewController *goIn = [[GoInViewController alloc] init];

        self.navigationItem.rightBarButtonItem.title = @"注销";

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:goIn];
        

        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }else {
         // 如果是登录状态  点击提示注销
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要注销么" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *aciont1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            UserModel *model = [[UserModel alloc] init];
            model = [[LXC_DataBaseHandle shareDataBaseHandle] seletLoginUserFromUserTab:1];
            
            [[LXC_DataBaseHandle shareDataBaseHandle] updateModelFromUserTab:model loginState:0];
            self.navigationItem.rightBarButtonItem.title = @"登录";

        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [alert addAction:action2];
        [alert addAction:aciont1];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
 

    
}
- (void)cancel:(UIBarButtonItem *)button {
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrTitle.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text = _arrTitle[indexPath.row];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSInteger goinState = [[LXC_DataBaseHandle shareDataBaseHandle] selectLoginStateFromUserTab:1];
    
    // 判断是否登录
    if (goinState) {
        
        // 进入我的活动
        if (indexPath.row == 0) {
            
            MyActiviCollection *activiCollection = [[MyActiviCollection alloc] init];
            [self.navigationController pushViewController:activiCollection animated:YES];
            [activiCollection release];
            
        }
        // 进入我的电影
        if (indexPath.row == 1) {
            
            MyMovieCollectionViewController *movieCollection = [[MyMovieCollectionViewController alloc] init];
            [self.navigationController pushViewController:movieCollection animated:YES];
            [movieCollection release];
            
        }
        // 清除缓存
        if (indexPath.row == 2) {

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要清除缓存么" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                // 清空数据库
                [[LXC_DataBaseHandle shareDataBaseHandle] deleteTable];
                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
            [alert addAction:action1];
            [alert addAction:action2];
            [self presentViewController:alert animated:YES completion:nil];
        }

    }else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请登陆后查看" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
